function $(id) {
    return document.getElementById(id);
}

window.onload = function () {

    findByItem();
}

function findByItem() {

    axios.get("/room/findByItem",{
        params:{
            address:$("address").value,
            state:$("roomState").value,
            bool:$("inRoom").value,
            type:$("roomType").value
        }
    }).then(resp =>{
        var str = "";
        resp.data.forEach(e =>{
            str += `<tr><td>${e.roomAddress}</td><td>${e.maxNum}</td>
<td>${e.roomState}</td>
<td>${e.type}</td>
<td><input type="button" value="删除" onclick="del(${e.roomId})">
<input type="button" value="查看房间信息" onclick="showRoomInfo(${e.roomId})">
<input type="button" value="查看房间设施" onclick="showUnitInfo(${e.roomId})"></td></tr>`;

            $("bodyData").innerHTML = str;
        })
    })
}

function del(id) {
    axios.get("/room/del",{
        params: {
            id
        }
    }).then(resp =>{
        if (resp.data == "ok"){
            findByItem();
        }
        else {
            alert("该房间还有人住不能删除")
        }
    })
}

function showAddRoom() {
    $("showDiv").style.visibility = 'visible';
    $("showDiv").style.width = document.documentElement.clientWidth+"px";
    $("showDiv").style.height = document.documentElement.clientHeight+"px";

}

function addRoom() {
    axios.get("/room/add",{
        params:{
            roomAddress: $("addAddress").value,
            maxNum:$("maxNum").value,
            type:$("addType").value
        }
    }).then(resp =>{
        if (resp.data == "ok"){
            $("showDiv").style.visibility = 'hidden';
            findByItem();
        }
    })
}

function no() {
    $("showDiv").style.visibility = 'hidden';
    $("showRoomDiv").style.visibility = 'hidden';
    $("showUnitDiv").style.visibility = 'hidden';
    findByItem();
}

function showRoomInfo(id) {

    $("showRoomDiv").style.visibility = 'visible';
    $("showRoomDiv").style.width = document.documentElement.clientWidth+"px";
    $("showRoomDiv").style.height = document.documentElement.clientHeight+"px";

    axios.get("/room/findById",{
        params:{
            id
        }
    }).then(resp =>{
        var roomBean = resp.data;
        $("addressInfo").innerHTML = roomBean.roomAddress;
        $("maxNumInfo").innerHTML = roomBean.maxNum;
        $("num").innerHTML = roomBean.num;
        $("state").innerHTML = roomBean.roomState;

        var str = "";
        roomBean.stuList.forEach(e =>{
            str += `<tr><td>${e.stuName}</td><td>${e.classBean}</td><td>${e.phone}</td></tr>`;
            $("stuInfo").innerHTML = str;
        })
    })
}

function showUnitInfo(id) {

    $("showUnitDiv").style.visibility = 'visible';
    $("showUnitDiv").style.width = document.documentElement.clientWidth+"px";
    $("showUnitDiv").style.height = document.documentElement.clientHeight+"px";

    axios.get("/room/findUnit",{
        roomId:id
    }).then(resp =>{
        var str = "";
        resp.data.forEach(e =>{
            str += `<tr><td>${e.unitInfo}</td><td>${e.date}</td><td>${e.unState}</td>
        <td><input type="button" value="添加设施记录" onclick="addUnit(${id})">
            <input type="button" value="处理设施记录" onclick="updateState(${e.unitId},${id})">
        </td></tr>`;

            $("unitInfo").innerHTML = str;
        })
    })
}

function addUnit(id) {
    axios.get("/room/addUnit",{
        params:{
            unitInfo:$("unitInfo").value,
            roomBean:id
        }
    }).then(resp =>{
        if (resp.data == "ok"){
            showUnitInfo(id);
        }
    })

}

function updateState(unitId,roomId) {

    axios.get("/room/updateState",{
        params:{
            unitId:unitId,
            roomId:roomId
        }
    }).then(resp =>{
        if (resp.data == "ok"){
            showUnitInfo(roomId);
        }
    })
}